{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "fef341c0",
   "metadata": {},
   "source": [
    "# python基础入门1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "589a1c03",
   "metadata": {},
   "source": [
    "## 数据类型与变量"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f8c6bb39",
   "metadata": {},
   "source": [
    " ### 数据类型\n",
    " Python中的数据类型可以分为以下八种基本类型：布尔型（bool）、整数型（int）、浮点数型（float）、字符串（String）、列表（List）、元组（Tuple）、集合（Set）和字典（Dictionary）。\n",
    " \n",
    "布尔型（bool），也称为布尔类型，是Python的一种基础数据类型。在Python中，布尔型用bool表示，bool类是int类的子类。它主要用来表示逻辑上的真或假，即存在或不存在的情况。具体来说，布尔类型提供了两个布尔值：True和False。其中，True代表真（对）或存在，False代表假（错）或不存在。\n",
    "\n",
    "整数型（int）：一种用于表示整数的数据类型。\n",
    "\n",
    "浮点数型（float）：一种用于表示浮点数的数据类型。\n",
    "\n",
    "字符串（String）： 字符串是由字符组成的不可变序列。可以用单引号(' ')、双引号(\" \")或三引号(''' '''或\"\"\" \"\"\")来表示字符串。\n",
    "\n",
    "列表（List）： 列表是一个有序的元素集合，可以包含任何类型的项目，并且可以进行修改。列表用方括号([])表示。\n",
    "\n",
    "元组（Tuple）： 元组与列表类似，但是元组是不可变的，一旦被声明，就不能修改。元组用圆括号(())表示。\n",
    "\n",
    "集合（Set）： 集合是一个无序的不重复元素序列。可以使用set()函数创建集合，或者使用大括号{}创建集合。注意：集合是无序的，且元素不能重复。\n",
    "\n",
    "字典（Dictionary）： 字典是一种可变的、无序的键值对集合。字典用大括号{}表示，每个元素是一个键值对。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "69643d93",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "bool"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 布尔型\n",
    "type(True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "831350ef",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "bool"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "223ba4a1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "int"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 整数型\n",
    "type(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "db944ce4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "float"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#浮点数\n",
    "type(1.0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "1f8f861d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "str"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#字符串\n",
    "type(\"hello\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "fa22f665",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "list"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#列表\n",
    "type([1,2,3,4])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "b97b515c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tuple"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#元组\n",
    "type((1,2,3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "3a6f024c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "set"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#集合\n",
    "type({1,2,3})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "1da33c02",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#字典\n",
    "    \n",
    "type({\"name\":\"jack\",\"age\":12})"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f1e67244",
   "metadata": {},
   "source": [
    "### 变量及赋值\n",
    "在Python编程语言中，变量是用来存储数据的容器。其基本概念、命名规范、作用域、类型等是编程的基础部分。Python中的变量类型多样，包括数字（如整数和浮点数）、字符串、列表、元组、字典等。\n",
    "\n",
    "创建变量时，需要指定变量名和值。例如，创建一个名为num的整数型变量，可以写成num = 10。这里，=是赋值符号，其左侧是变量名，右侧是要赋给变量的值。\n",
    "\n",
    "Python还允许多重赋值，即一次为多个变量赋值。例如，a, b = 1, 2就同时创建了两个变量a和b，并分别赋值为1和2。此外，Python也支持列表和字典这样的复合数据类型，可以为这些复合数据类型的每一个元素或键值对分别赋值。\n",
    "\n",
    "需要注意的是，Python中的变量其实是对对象的引用，我们通过变量名来操作对象。当一个变量被赋予一个新的对象引用时，原对象将不再被使用。此外，Python还有全局变量和局部变量的概念，全局变量是在函数外部定义的变量，其作用域是整个程序；而局部变量是在函数内部定义的变量，其作用域仅限于函数内部。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "3caad051",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'a' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_1708/2167009006.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0ma\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name 'a' is not defined"
     ]
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "64396877",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "84870512",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "005aae1c",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 1.6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "c29108bc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "float"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "ed7e8fd0",
   "metadata": {},
   "outputs": [],
   "source": [
    "a,b=1,2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "83297d1f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "237131e1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "id": "614e1164",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 2)"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a,b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "3a8badab",
   "metadata": {},
   "outputs": [],
   "source": [
    "mystr=\"nihao\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "766453dd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'nihao'"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mystr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "ac7a3f44",
   "metadata": {},
   "outputs": [],
   "source": [
    "mystr=\"hello\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "b6218f2d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'hello'"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mystr"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "456ee5dc",
   "metadata": {},
   "source": [
    "## 布尔类型与逻辑运算\n",
    "在Python编程语言中，布尔类型（bool）是一种基本的数据类型，它只有两个值：True和False。这种类型的变量通常用于表示逻辑判断的结果，比如真假、存在与否等。\n",
    "\n",
    "与布尔类型密切相关的是逻辑运算。Python支持的逻辑运算主要有以下三种：\n",
    "\n",
    "与运算（and）：只有当两个布尔值都为True时，结果才为True。例如，True and True的结果为True，而True and False或者False and True的结果都为False。\n",
    "\n",
    "或运算（or）：只要有一个布尔值为True，结果就为True。例如，True or True的结果为True，而True or False的结果为True，False or False的结果为False。\n",
    "\n",
    "非运算（not）：对一个布尔值取反，True变为False，False变为True。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4daa2be6",
   "metadata": {},
   "source": [
    "### 与运算（and）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "60551ff7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "True and True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "id": "eca30d2d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "True and False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "id": "aec37137",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "False and True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "id": "015f84eb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "False and False"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b0c2aec9",
   "metadata": {},
   "source": [
    "### 或运算（or）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "id": "88bb083f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "True or True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "id": "07169961",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "True or False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "id": "dedbee62",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "False or True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "bfad30b2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n"
     ]
    }
   ],
   "source": [
    "a=False or False"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7d088284",
   "metadata": {},
   "source": [
    "### 非运算（not）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "id": "e4ff8207",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 101,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "not True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "67e53011",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "not False"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a57682e7",
   "metadata": {},
   "source": [
    "## 输入输出"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "074668ca",
   "metadata": {},
   "source": [
    "### 输出"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "id": "815f8ce7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello World!\n"
     ]
    }
   ],
   "source": [
    "#输出字符串\n",
    "print(\"Hello World!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "id": "225b0d6f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n"
     ]
    }
   ],
   "source": [
    "#输出变量\n",
    "a = 1\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "id": "ddf674a9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "#输出算式\n",
    "print(1+1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "id": "96c70637",
   "metadata": {},
   "outputs": [],
   "source": [
    "#格式化输出（灵活的输出）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "id": "d5cf9d58",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "this is a fruit\n"
     ]
    }
   ],
   "source": [
    "print(\"this is a {}\".format(\"fruit\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "id": "5df2927e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "this are apple and banana\n"
     ]
    }
   ],
   "source": [
    "print(\"this are {} and {}\".format(\"apple\",\"banana\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "id": "95b0491a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "this are banana and apple\n"
     ]
    }
   ],
   "source": [
    "print(\"this are {} and {}\".format(\"banana\",\"apple\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "id": "ee32d16d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "this are apple and banana\n"
     ]
    }
   ],
   "source": [
    "fruit1=\"apple\"\n",
    "fruit2=\"banana\"\n",
    "\n",
    "print(\"this are {} and {}\".format(fruit1,fruit2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "id": "947889ea",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "this are apple and banana\n"
     ]
    }
   ],
   "source": [
    "fruit1=\"apple\"\n",
    "fruit2=\"banana\"\n",
    "\n",
    "print(\"this are {fruit1} and {fruit2}\".format(fruit1=fruit1,fruit2=fruit2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 134,
   "id": "63b52110",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "this are banana and apple\n"
     ]
    }
   ],
   "source": [
    "fruit1=\"apple\"\n",
    "fruit2=\"banana\"\n",
    "\n",
    "print(\"this are {fruit1} and {fruit2}\".format(fruit1=fruit2,fruit2=fruit1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 136,
   "id": "70f4893f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "this are banana and apple\n"
     ]
    }
   ],
   "source": [
    "fruit1=\"apple\"\n",
    "fruit2=\"banana\"\n",
    "\n",
    "print(\"this are {fruit_1} and {fruit_2}\".format(fruit_1=fruit2,fruit_2=fruit1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 138,
   "id": "8e86f9a4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "小明 is 3 years old.\n"
     ]
    }
   ],
   "source": [
    "name=\"小明\"\n",
    "age=3\n",
    "print(\"{name} is {age} years old.\".format(name=name,age=age))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0dd0df1b",
   "metadata": {},
   "source": [
    "### 输入"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 141,
   "id": "783fd462",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入您的手机号：123123\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'123123'"
      ]
     },
     "execution_count": 141,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "input(\"请输入您的手机号：\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 142,
   "id": "7af8e6ca",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入您的手机号：123\n"
     ]
    }
   ],
   "source": [
    "tel=input(\"请输入您的手机号：\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 145,
   "id": "cb1b92f2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'123'"
      ]
     },
     "execution_count": 145,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tel"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cd0d7bff",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9fbcbf60",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
